import request from '@/utils/request'

export const fetchCaptcha = (phone) => {
  return request({
    url: 'validatecode/send4Register',
    method: 'post',
    data: new URLSearchParams({ phone })
  })
}

export const submitRegister = (registerData) => {
  // 借助于UrlSearchParams完成传递
  const params = new URLSearchParams();
  for (let key in registerData) {
    params.append(key, registerData[key]);
  }
  return request({
    url: 'user/register',
    method: 'post',
    params
  })
}
// 注册成功之后流程与登录成功类似
// 提交登录表单，后端验证通过后要返回一个标志用户登录成功的标志位，同时把当前用户的信息返回给前端
// 标志位以isLogin存储在sessionStorage中，用户数据存储到pinia-info里
export const submitLogin = (data) => {
  const params = new URLSearchParams();
  for (let key in data) {
    params.append(key, data[key]);
  }
  return request({
    url: 'user/login',
    method: 'post',
    params
  })
}